//index.js

import API from "../../data/api";
import Util from "../../utils/util";

//获取应用实例
const App = getApp();
const Config = App.Config;

Page({
  data: {
    listData: [],
    page: 0,
    HOSTIMG: Config.HOSTIMG,
  },

  onReady: function () {
    var that = this;
    // 初始化数据
    that.setData({
      listData: [],
      page: 0,
    });
    // 获取文章内容
    that.getArticle(0)
  },

  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    var that = this;

    that.setData({
      listData: [],
    });
    that.getArticle(0);
    wx.stopPullDownRefresh();
  },

  onShareAppMessage: function () {
    // 用户点击右上角分享
    return {
      title: '北理微助手', // 分享标题
      desc: '北理微助手', // 分享描述
      path: '/pages/index/index' // 分享路径
    }
  },

  onReachBottom: function () {
    // 页面上拉触底事件的处理函数
    var that = this;
    that.setData({
      page: that.data.page + 1
    });
    that.getArticle(that.data.page);
  },


  // 获取文章内容
  getArticle: function (page) {
    var that = this;
    
    API.getArticles(page)
      .then((res) => {
        // console.log(res);
        if (res.data.error == Config.ERR.OK) {
          if (that.data.page == 0) {
            that.setData({
              listData: res.data.data
            })
          } else {
            var newdata = that.data.listData
            newdata.push(...res.data.data);
            that.setData({
              listData: newdata
            })
          }
        } else if (res.data.error == Config.ERR.ER) {
          // Util.UI.loading(1000, res.data.data);
        }
      });
  },
// 跳转校内电话查询
  goToAvatar: function () {
    wx.navigateTo({
      url: '../avatar/avatar',
    })
  },
  // 跳转校内电话查询
  goToCampusTel: function () {
    wx.navigateTo({
      url: '../campusTel/campusTel',
    })
  },

  // 跳转校外住宿查询
  goToLost: function () {
    wx.navigateTo({
      url: '../community/community',
    })
  },


  // 跳转成绩查询
  goToScore: function () {
    wx.navigateTo({
      url: '../score/score',
    })
  },


  // 跳转课表查询
  goToSchedule: function () {
    wx.navigateTo({
      url: '../schedule/schedule',
    })
  },

  // 跳转文章
  goToArticle: function (event) {
    // console.log(event.currentTarget.dataset.articleid);
    wx.navigateTo({
      url: '../article/article?articleid=' + event.currentTarget.dataset.articleid,
    })
  }

})


